<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    //个人设置页面
    public function setting(){
        return view('user.setting');
    }
    //个人设置操作
    public function settingStore(){

    }
    //个人页面展示页
    public function show(User $user){
        //这个人的信息（关注，粉丝，文章数）
        $user = User::withCount(['posts','stars','fans'])->find($user->id);
        //这个人前10条文章
        $posts = $user->posts()->orderBy('created_at','desc')->take(10)->get();
        //这个人关注的用户（关注的用户的 关注、粉丝、文章数）
        $stars = $user->stars;
        $susers = User::whereIn('id',$stars->pluck('star_id'))->withCount(['posts','stars','fans'])->get();
        //z这个人的粉丝用户（粉丝用户的 关注、粉丝、文章数）
        $fans = $user->fans;
        $fusers = User::whereIn('id',$fans->pluck('fan_id'))->withCount(['posts','stars','fans'])->get();

        return view('user.show',compact('user','posts','susers','fusers'));
    }

    public function fan(User $user){
        $me = Auth::user();
        $me->doFan($user->id);
        return [
          'error' =>0,
          'msg'   =>'关注成功'
        ];
    }

    public function unfan(User $user){
        $me = Auth::user();
        $me->doUnFan($user->id);
        return response()->json( [
            'error' =>0,
            'msg'   =>'取消关注成功'
        ]);
    }
}
